package com.study.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * @创建时间 2020/4/20 9:34
 */
@Slf4j
@Controller
@RequestMapping("tools")
@Api(tags = "系统功能")
public class SysToolController {

    //	获取配置文件中的值
    @Resource
    Environment environment;
    @Value("${sys.config.pic.savepath}")
    String picPath;

    /**
     * * 在不知道文件名情况下获取上传文件
     *
     * @param file
     * @param request
     * @return
     */
    @ApiOperation("在不知道文件名情况下获取上传文件")
    @ApiImplicitParam(name = "is", value = "上传图片a", allowMultiple = true, dataType = "file")
    @RequestMapping(value = "upfile", method = RequestMethod.POST)
    public @ResponseBody
    String uploadOssFile(MultipartFile file, HttpServletRequest request, String is) {
        String picUrl = environment.getProperty("sys.config.pic.baseurl");
        log.info(StringUtils.repeat('-', 28));
        // String url = ossService.uploadOssFile(file);
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;

        Map<String, MultipartFile> fileMap = mr.getFileMap();
        Set<Map.Entry<String, MultipartFile>> entries = fileMap.entrySet();
        String url = null;
        for (Map.Entry<String, MultipartFile> entry : entries) {
            MultipartFile multipartFile = entry.getValue();
            String key = entry.getKey();
            url = multipartFile.getOriginalFilename();
            log.info("key-value|表单字段和文件名分别是|:{},{}", key, multipartFile.getOriginalFilename());
            try {
                // TODO 此方法无法自动创建不存在的文件夹
                // multipartFile.transferTo(new File(picPath +
                // multipartFile.getOriginalFilename()));

                // 文件保存
                url = com.study.utils.SysUtils.getDateStrSpan_() + "/" + com.study.utils.SysUtils.getDate4() + multipartFile.getOriginalFilename();
                // TODO 可以自动创建不存在的文件夹
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(picPath + url));
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.format("%s/%s", picUrl, url);
    }


    /*@RequestMapping("/allnew")
    @ResponseBody
    public Object getMenu() {
        log.info("menu");
        // 只支持Integer.class String.class
        // 这种单数据类型的，自己定义的Bean不支|https://blog.csdn.net/J_mani/article/details/83896372?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
        // List<Menu> queryForList = jdbcTemplate.queryForList("select * from t_menu",
        // Menu.class);
        List<Menu> queryForList = jdbcTemplate.query("select id,pId ,name,url,icon from t_menu", new BeanPropertyRowMapper(Menu.class));
        System.out.println("AdminController.getALL()");
        log.info(queryForList.toString());
        return queryForList;
    }*/
}
